home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / gperf / gperf.info (.txt) < prev    next >
GNU Info File  |  1994-10-24  |  52KB  |  925 lines

  1. This is Info file gperf.info, produced by Makeinfo-1.55 from the input
  2. file ./gperf.texi.
  3. START-INFO-DIR-ENTRY
  4. * Gperf: (gperf).                Perfect Hash Function Generator.
  5. END-INFO-DIR-ENTRY
  6.    This file documents the features of the GNU Perfect Hash Function
  7. Generator
  8.    Copyright (C) 1989 Free Software Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the section entitled "GNU General Public License" is included
  15. exactly as in the original, and provided that the entire resulting
  16. derived work is distributed under the terms of a permission notice
  17. identical to this one.
  18.    Permission is granted to copy and distribute translations of this
  19. manual into another language, under the above conditions for modified
  20. versions, except that the section entitled "GNU `gperf' General Public
  21. License" an d this permission notice may be included in translations
  22. approved by the Free Software Foundation instead of in the original
  23. English.
  24. File: gperf.info,  Node: Top,  Next: Copying,  Prev: (dir),  Up: (dir)
  25. Introduction
  26. ************
  27.    This manual documents the GNU `gperf' perfect hash function generator
  28. utility, focusing on its features and how to use them, and how to report
  29. bugs.
  30. * Menu:
  31. * Copying::            GNU `gperf' General Public License says
  32.                     how you can copy and share `gperf'.
  33. * Contributors::        People who have contributed to `gperf'.
  34. * Motivation::            Static search structures and GNU GPERF.
  35. * Search Structures::        Static search structures and GNU `gperf'
  36. * Description::            High-level discussion of how GPERF functions.
  37. * Options::            A description of options to the program.
  38. * Bugs::            Known bugs and limitations with GPERF.
  39. * Projects::            Things still left to do.
  40. * Implementation::        Implementation Details for GNU GPERF.
  41. * Bibliography::        Material Referenced in this Report.
  42.  -- The Detailed Node Listing --
  43. High-Level Description of GNU `gperf'
  44. * Input Format::        Input Format to `gperf'
  45. * Output Format::        Output Format for Generated C Code with `gperf'
  46. Input Format to `gperf'
  47. * Declarations::        `struct' Declarations and C Code Inclusion.
  48. * Keywords::            Format for Keyword Entries.
  49. * Functions::            Including Additional C Functions.
  50. File: gperf.info,  Node: Copying,  Next: Contributors,  Prev: Top,  Up: Top
  51. GNU GENERAL PUBLIC LICENSE
  52. **************************
  53.                        Version 1, February 1989
  54.      Copyright (C) 1989 Free Software Foundation, Inc.
  55.      675 Mass Ave, Cambridge, MA 02139, USA
  56.      
  57.      Everyone is permitted to copy and distribute verbatim copies
  58.      of this license document, but changing it is not allowed.
  59. Preamble
  60. ========
  61.    The license agreements of most software companies try to keep users
  62. at the mercy of those companies.  By contrast, our General Public
  63. License is intended to guarantee your freedom to share and change free
  64. software--to make sure the software is free for all its users.  The
  65. General Public License applies to the Free Software Foundation's
  66. software and to any other program whose authors commit to using it.
  67. You can use it for your programs, too.
  68.    When we speak of free software, we are referring to freedom, not
  69. price.  Specifically, the General Public License is designed to make
  70. sure that you have the freedom to give away or sell copies of free
  71. software, that you receive source code or can get it if you want it,
  72. that you can change the software or use pieces of it in new free
  73. programs; and that you know you can do these things.
  74.    To protect your rights, we need to make restrictions that forbid
  75. anyone to deny you these rights or to ask you to surrender the rights.
  76. These restrictions translate to certain responsibilities for you if you
  77. distribute copies of the software, or if you modify it.
  78.    For example, if you distribute copies of a such a program, whether
  79. gratis or for a fee, you must give the recipients all the rights that
  80. you have.  You must make sure that they, too, receive or can get the
  81. source code.  And you must tell them their rights.
  82.    We protect your rights with two steps: (1) copyright the software,
  83. and (2) offer you this license which gives you legal permission to copy,
  84. distribute and/or modify the software.
  85.    Also, for each author's protection and ours, we want to make certain
  86. that everyone understands that there is no warranty for this free
  87. software.  If the software is modified by someone else and passed on, we
  88. want its recipients to know that what they have is not the original, so
  89. that any problems introduced by others will not reflect on the original
  90. authors' reputations.
  91.    The precise terms and conditions for copying, distribution and
  92. modification follow.
  93.                          TERMS AND CONDITIONS
  94.   1. This License Agreement applies to any program or other work which
  95.      contains a notice placed by the copyright holder saying it may be
  96.      distributed under the terms of this General Public License.  The
  97.      "Program", below, refers to any such program or work, and a "work
  98.      based on the Program" means either the Program or any work
  99.      containing the Program or a portion of it, either verbatim or with
  100.      modifications.  Each licensee is addressed as "you".
  101.   2. You may copy and distribute verbatim copies of the Program's source
  102.      code as you receive it, in any medium, provided that you
  103.      conspicuously and appropriately publish on each copy an
  104.      appropriate copyright notice and disclaimer of warranty; keep
  105.      intact all the notices that refer to this General Public License
  106.      and to the absence of any warranty; and give any other recipients
  107.      of the Program a copy of this General Public License along with
  108.      the Program.  You may charge a fee for the physical act of
  109.      transferring a copy.
  110.   3. You may modify your copy or copies of the Program or any portion of
  111.      it, and copy and distribute such modifications under the terms of
  112.      Paragraph 1 above, provided that you also do the following:
  113.         * cause the modified files to carry prominent notices stating
  114.           that you changed the files and the date of any change; and
  115.         * cause the whole of any work that you distribute or publish,
  116.           that in whole or in part contains the Program or any part
  117.           thereof, either with or without modifications, to be licensed
  118.           at no charge to all third parties under the terms of this
  119.           General Public License (except that you may choose to grant
  120.           warranty protection to some or all third parties, at your
  121.           option).
  122.         * If the modified program normally reads commands interactively
  123.           when run, you must cause it, when started running for such
  124.           interactive use in the simplest and most usual way, to print
  125.           or display an announcement including an appropriate copyright
  126.           notice and a notice that there is no warranty (or else,
  127.           saying that you provide a warranty) and that users may
  128.           redistribute the program under these conditions, and telling
  129.           the user how to view a copy of this General Public License.
  130.         * You may charge a fee for the physical act of transferring a
  131.           copy, and you may at your option offer warranty protection in
  132.           exchange for a fee.
  133.      Mere aggregation of another independent work with the Program (or
  134.      its derivative) on a volume of a storage or distribution medium
  135.      does not bring the other work under the scope of these terms.
  136.   4. You may copy and distribute the Program (or a portion or
  137.      derivative of it, under Paragraph 2) in object code or executable
  138.      form under the terms of Paragraphs 1 and 2 above provided that you
  139.      also do one of the following:
  140.         * accompany it with the complete corresponding machine-readable
  141.           source code, which must be distributed under the terms of
  142.           Paragraphs 1 and 2 above; or,
  143.         * accompany it with a written offer, valid for at least three
  144.           years, to give any third party free (except for a nominal
  145.           charge for the cost of distribution) a complete
  146.           machine-readable copy of the corresponding source code, to be
  147.           distributed under the terms of Paragraphs 1 and 2 above; or,
  148.         * accompany it with the information you received as to where the
  149.           corresponding source code may be obtained.  (This alternative
  150.           is allowed only for noncommercial distribution and only if you
  151.           received the program in object code or executable form alone.)
  152.      Source code for a work means the preferred form of the work for
  153.      making modifications to it.  For an executable file, complete
  154.      source code means all the source code for all modules it contains;
  155.      but, as a special exception, it need not include source code for
  156.      modules which are standard libraries that accompany the operating
  157.      system on which the executable file runs, or for standard header
  158.      files or definitions files that accompany that operating system.
  159.   5. You may not copy, modify, sublicense, distribute or transfer the
  160.      Program except as expressly provided under this General Public
  161.      License.  Any attempt otherwise to copy, modify, sublicense,
  162.      distribute or transfer the Program is void, and will automatically
  163.      terminate your rights to use the Program under this License.
  164.      However, parties who have received copies, or rights to use
  165.      copies, from you under this General Public License will not have
  166.      their licenses terminated so long as such parties remain in full
  167.      compliance.
  168.   6. By copying, distributing or modifying the Program (or any work
  169.      based on the Program) you indicate your acceptance of this license
  170.      to do so, and all its terms and conditions.
  171.   7. Each time you redistribute the Program (or any work based on the
  172.      Program), the recipient automatically receives a license from the
  173.      original licensor to copy, distribute or modify the Program
  174.      subject to these terms and conditions.  You may not impose any
  175.      further restrictions on the recipients' exercise of the rights
  176.      granted herein.
  177.   8. The Free Software Foundation may publish revised and/or new
  178.      versions of the General Public License from time to time.  Such
  179.      new versions will be similar in spirit to the present version, but
  180.      may differ in detail to address new problems or concerns.
  181.      Each version is given a distinguishing version number.  If the
  182.      Program specifies a version number of the license which applies to
  183.      it and "any later version", you have the option of following the
  184.      terms and conditions either of that version or of any later
  185.      version published by the Free Software Foundation.  If the Program
  186.      does not specify a version number of the license, you may choose
  187.      any version ever published by the Free Software Foundation.
  188.   9. If you wish to incorporate parts of the Program into other free
  189.      programs whose distribution conditions are different, write to the
  190.      author to ask for permission.  For software which is copyrighted
  191.      by the Free Software Foundation, write to the Free Software
  192.      Foundation; we sometimes make exceptions for this.  Our decision
  193.      will be guided by the two goals of preserving the free status of
  194.      all derivatives of our free software and of promoting the sharing
  195.      and reuse of software generally.
  196.                                 NO WARRANTY
  197.  10. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
  198.      WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
  199.      LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  200.      HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
  201.      WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
  202.      NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  203.      FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE
  204.      QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  205.      PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  206.      SERVICING, REPAIR OR CORRECTION.
  207.  11. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  208.      WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
  209.      MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
  210.      LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
  211.      INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  212.      INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  213.      DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
  214.      OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
  215.      OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  216.      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  217.                       END OF TERMS AND CONDITIONS
  218. Appendix: How to Apply These Terms to Your New Programs
  219. =======================================================
  220.    If you develop a new program, and you want it to be of the greatest
  221. possible use to humanity, the best way to achieve this is to make it
  222. free software which everyone can redistribute and change under these
  223. terms.
  224.    To do so, attach the following notices to the program.  It is safest
  225. to attach them to the start of each source file to most effectively
  226. convey the exclusion of warranty; and each file should have at least the
  227. "copyright" line and a pointer to where the full notice is found.
  228.      ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
  229.      Copyright (C) 19YY  NAME OF AUTHOR
  230.      
  231.      This program is free software; you can redistribute it and/or modify
  232.      it under the terms of the GNU General Public License as published by
  233.      the Free Software Foundation; either version 1, or (at your option)
  234.      any later version.
  235.      
  236.      This program is distributed in the hope that it will be useful,
  237.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  238.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  239.      GNU General Public License for more details.
  240.      
  241.      You should have received a copy of the GNU General Public License
  242.      along with this program; if not, write to the Free Software
  243.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  244.    Also add information on how to contact you by electronic and paper
  245. mail.
  246.    If the program is interactive, make it output a short notice like
  247. this when it starts in an interactive mode:
  248.      Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
  249.      Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  250.      This is free software, and you are welcome to redistribute it
  251.      under certain conditions; type `show c' for details.
  252.    The hypothetical commands `show w' and `show c' should show the
  253. appropriate parts of the General Public License.  Of course, the
  254. commands you use may be called something other than `show w' and `show
  255. c'; they could even be mouse-clicks or menu items--whatever suits your
  256. program.
  257.    You should also get your employer (if you work as a programmer) or
  258. your school, if any, to sign a "copyright disclaimer" for the program,
  259. if necessary.  Here a sample; alter the names:
  260.      Yoyodyne, Inc., hereby disclaims all copyright interest in the
  261.      program `Gnomovision' (a program to direct compilers to make passes
  262.      at assemblers) written by James Hacker.
  263.      
  264.      SIGNATURE OF TY COON, 1 April 1989
  265.      Ty Coon, President of Vice
  266.    That's all there is to it!
  267. File: gperf.info,  Node: Contributors,  Next: Motivation,  Prev: Copying,  Up: Top
  268. Contributors to GNU `gperf' Utility
  269. ***********************************
  270.    * The GNU `gperf' perfect hash function generator utility was
  271.      originally written in GNU C++ by Douglas C. Schmidt.  It is now
  272.      also available in a highly-portable "old-style" C version.  The
  273.      general idea for the perfect hash function generator was inspired
  274.      by Keith Bostic's algorithm written in C, and distributed to
  275.      net.sources around 1984.  The current program is a heavily
  276.      modified, enhanced, and extended implementation of Keith's basic
  277.      idea, created at the University of California, Irvine.  Bugs,
  278.      patches, and suggestions should be reported to schmidt at
  279.      ics.uci.edu.
  280.    * Special thanks is extended to Michael Tiemann and Doug Lea, for
  281.      providing a useful compiler, and for giving me a forum to exhibit
  282.      my creation.
  283.      In addition, Adam de Boor and Nels Olson provided many tips and
  284.      insights that greatly helped improve the quality and functionality
  285.      of `gperf'.
  286. File: gperf.info,  Node: Motivation,  Next: Search Structures,  Prev: Contributors,  Up: Top
  287. Introduction
  288. ************
  289.    `gperf' is a perfect hash function generator written in C++.  It
  290. transforms an *n* element user-specified keyword set *W* into a perfect
  291. hash function *F*.  *F* uniquely maps keywords in *W* onto the range
  292. 0..*k*, where *k* >= *n*.  If *k = n* then *F* is a *minimal* perfect
  293. hash function.  `gperf' generates a 0..*k* element static lookup table
  294. and a pair of C functions.  These functions determine whether a given
  295. character string *s* occurs in *W*, using at most one probe into the
  296. lookup table.
  297.    `gperf' currently generates the reserved keyword recognizer for
  298. lexical analyzers in several production and research compilers and
  299. language processing tools, including GNU C, GNU C++, GNU Pascal, GNU
  300. Modula 3, and GNU indent.  Complete C++ source code for `gperf' is
  301. available via anonymous ftp from ics.uci.edu.  `gperf' also is
  302. distributed along with the GNU libg++ library.  A highly portable,
  303. functionally equivalent K&R C version of `gperf' is archived in
  304. comp.sources.unix, volume 20.  Finally, a paper describing `gperf''s
  305. design and implementation in greater detail is available in the Second
  306. USENIX C++ Conference proceedings.
  307. File: gperf.info,  Node: Search Structures,  Next: Description,  Prev: Motivation,  Up: Top
  308. Static search structures and GNU `gperf'
  309. ****************************************
  310.    A "static search structure" is an Abstract Data Type with certain
  311. fundamental operations, *e.g.*, *initialize*, *insert*, and *retrieve*.
  312. Conceptually, all insertions occur before any retrievals.  In
  313. practice, `gperf' generates a `static' array containing search set
  314. keywords and any associated attributes specified by the user.  Thus,
  315. there is essentially no execution-time cost for the insertions.  It is
  316. a useful data structure for representing *static search sets*.  Static
  317. search sets occur frequently in software system applications.  Typical
  318. static search sets include compiler reserved words, assembler
  319. instruction opcodes, and built-in shell interpreter commands.  Search
  320. set members, called "keywords", are inserted into the structure only
  321. once, usually during program initialization, and are not generally
  322. modified at run-time.
  323.    Numerous static search structure implementations exist, *e.g.*,
  324. arrays, linked lists, binary search trees, digital search tries, and
  325. hash tables.  Different approaches offer trade-offs between space
  326. utilization and search time efficiency.  For example, an *n* element
  327. sorted array is space efficient, though the average-case time
  328. complexity for retrieval operations using binary search is proportional
  329. to log *n*.  Conversely, hash table implementations often locate a
  330. table entry in constant time, but typically impose additional memory
  331. overhead and exhibit poor worst case performance.
  332.    *Minimal perfect hash functions* provide an optimal solution for a
  333. particular class of static search sets.  A minimal perfect hash
  334. function is defined by two properties:
  335.    * It allows keyword recognition in a static search set using at most
  336.      *one* probe into the hash table.  This represents the "perfect"
  337.      property.
  338.    * The actual memory allocated to store the keywords is precisely
  339.      large enough for the keyword set, and *no larger*.  This is the
  340.      "minimal" property.
  341.    For most applications it is far easier to generate *perfect* hash
  342. functions than *minimal perfect* hash functions.  Moreover, non-minimal
  343. perfect hash functions frequently execute faster than minimal ones in
  344. practice.  This phenomena occurs since searching a sparse keyword table
  345. increases the probability of locating a "null" entry, thereby reducing
  346. string comparisons.  `gperf''s default behavior generates
  347. *near-minimal* perfect hash functions for keyword sets.  However,
  348. `gperf' provides many options that permit user control over the degree
  349. of minimality and perfection.
  350.    Static search sets often exhibit relative stability over time.  For
  351. example, Ada's 63 reserved words have remained constant for nearly a
  352. decade.  It is therefore frequently worthwhile to expend concerted
  353. effort building an optimal search structure *once*, if it subsequently
  354. receives heavy use multiple times.  `gperf' removes the drudgery
  355. associated with constructing time- and space-efficient search
  356. structures by hand.  It has proven a useful and practical tool for
  357. serious programming projects.  Output from `gperf' is currently used in
  358. several production and research compilers, including GNU C, GNU C++,
  359. GNU Pascal, and GNU Modula 3.  The latter two compilers are not yet
  360. part of the official GNU distribution.  Each compiler utilizes `gperf'
  361. to automatically generate static search structures that efficiently
  362. identify their respective reserved keywords.
  363. File: gperf.info,  Node: Description,  Next: Options,  Prev: Search Structures,  Up: Top
  364. High-Level Description of GNU `gperf'
  365. *************************************
  366. * Menu:
  367. * Input Format::        Input Format to `gperf'
  368. * Output Format::        Output Format for Generated C Code with `gperf'
  369.    The perfect hash function generator `gperf' reads a set of
  370. "keywords" from a "keyfile" (or from the standard input by default).
  371. It attempts to derive a perfect hashing function that recognizes a
  372. member of the "static keyword set" with at most a single probe into the
  373. lookup table.  If `gperf' succeeds in generating such a function it
  374. produces a pair of C source code routines that perform hashing and
  375. table lookup recognition.  All generated C code is directed to the
  376. standard output.  Command-line options described below allow you to
  377. modify the input and output format to `gperf'.
  378.    By default, `gperf' attempts to produce time-efficient code, with
  379. less emphasis on efficient space utilization.  However, several options
  380. exist that permit trading-off execution time for storage space and vice
  381. versa.  In particular, expanding the generated table size produces a
  382. sparse search structure, generally yielding faster searches.
  383. Conversely, you can direct `gperf' to utilize a C `switch' statement
  384. scheme that minimizes data space storage size.  Furthermore, using a C
  385. `switch' may actually speed up the keyword retrieval time somewhat.
  386. Actual results depend on your C compiler, of course.
  387.    In general, `gperf' assigns values to the characters it is using for
  388. hashing until some set of values gives each keyword a unique value.  A
  389. helpful heuristic is that the larger the hash value range, the easier
  390. it is for `gperf' to find and generate a perfect hash function.
  391. Experimentation is the key to getting the most from `gperf'.
  392. File: gperf.info,  Node: Input Format,  Next: Output Format,  Prev: Description,  Up: Description
  393. Input Format to `gperf'
  394. =======================
  395.    You can control the input keyfile format by varying certain
  396. command-line arguments, in particular the `-t' option.  The input's
  397. appearance is similar to GNU utilities `flex' and `bison' (or UNIX
  398. utilities `lex' and `yacc').  Here's an outline of the general format:
  399.      declarations
  400.      %%
  401.      keywords
  402.      %%
  403.      functions
  404.    *Unlike* `flex' or `bison', all sections of `gperf''s input are
  405. optional.  The following sections describe the input format for each
  406. section.
  407. * Menu:
  408. * Declarations::        `struct' Declarations and C Code Inclusion.
  409. * Keywords::            Format for Keyword Entries.
  410. * Functions::            Including Additional C Functions.
  411. File: gperf.info,  Node: Declarations,  Next: Keywords,  Prev: Input Format,  Up: Input Format
  412. `struct' Declarations and C Code Inclusion
  413. ------------------------------------------
  414.    The keyword input file optionally contains a section for including
  415. arbitrary C declarations and definitions, as well as provisions for
  416. providing a user-supplied `struct'.  If the `-t' option *is* enabled,
  417. you *must* provide a C `struct' as the last component in the
  418. declaration section from the keyfile file.  The first field in this
  419. struct must be a `char *' identifier called "name," although it is
  420. possible to modify this field's name with the `-K' option described
  421. below.
  422.    Here is simple example, using months of the year and their
  423. attributes as input:
  424.      struct months { char *name; int number; int days; int leap_days; };
  425.      %%
  426.      january,   1, 31, 31
  427.      february,  2, 28, 29
  428.      march,     3, 31, 31
  429.      april,     4, 30, 30
  430.      may,       5, 31, 31
  431.      june,      6, 30, 30
  432.      july,      7, 31, 31
  433.      august,    8, 31, 31
  434.      september, 9, 30, 30
  435.      october,  10, 31, 31
  436.      november, 11, 30, 30
  437.      december, 12, 31, 31
  438.    Separating the `struct' declaration from the list of key words and
  439. other fields are a pair of consecutive percent signs, `%%', appearing
  440. left justified in the first column, as in the UNIX utility `lex'.
  441.    Using a syntax similar to GNU utilities `flex' and `bison', it is
  442. possible to directly include C source text and comments verbatim into
  443. the generated output file.  This is accomplished by enclosing the region
  444. inside left-justified surrounding `%{', `%}' pairs.  Here is an input
  445. fragment based on the previous example that illustrates this feature:
  446.      %{
  447.      #include <assert.h>
  448.      /* This section of code is inserted directly into the output. */
  449.      int return_month_days (struct months *months, int is_leap_year);
  450.      %}
  451.      struct months { char *name; int number; int days; int leap_days; };
  452.      %%
  453.      january,   1, 31, 31
  454.      february,  2, 28, 29
  455.      march,     3, 31, 31
  456.      ...
  457.    It is possible to omit the declaration section entirely.  In this
  458. case the keyfile begins directly with the first keyword line, *e.g.*:
  459.      january,   1, 31, 31
  460.      february,  2, 28, 29
  461.      march,     3, 31, 31
  462.      april,     4, 30, 30
  463.      ...
  464. File: gperf.info,  Node: Keywords,  Next: Functions,  Prev: Declarations,  Up: Input Format
  465. Format for Keyword Entries
  466. --------------------------
  467.    The second keyfile format section contains lines of keywords and any
  468. associated attributes you might supply.  A line beginning with `#' in
  469. the first column is considered a comment.  Everything following the `#'
  470. is ignored, up to and including the following newline.
  471.    The first field of each non-comment line is always the key itself.
  472. It should be given as a simple name, *i.e.*, without surrounding string
  473. quotation marks, and be left-justified flush against the first column.
  474. In this context, a "field" is considered to extend up to, but not
  475. include, the first blank, comma, or newline.  Here is a simple example
  476. taken from a partial list of C reserved words:
  477.      # These are a few C reserved words, see the c.`gperf' file
  478.      # for a complete list of ANSI C reserved words.
  479.      unsigned
  480.      sizeof
  481.      switch
  482.      signed
  483.      if
  484.      default
  485.      for
  486.      while
  487.      return
  488.    Note that unlike `flex' or `bison' the first `%%' marker may be
  489. elided if the declaration section is empty.
  490.    Additional fields may optionally follow the leading keyword.  Fields
  491. should be separated by commas, and terminate at the end of line.  What
  492. these fields mean is entirely up to you; they are used to initialize the
  493. elements of the user-defined `struct' provided by you in the
  494. declaration section.  If the `-t' option is *not* enabled these fields
  495. are simply ignored.  All previous examples except the last one contain
  496. keyword attributes.
  497. File: gperf.info,  Node: Functions,  Prev: Keywords,  Up: Input Format
  498. Including Additional C Functions
  499. --------------------------------
  500.    The optional third section also corresponds closely with conventions
  501. found in `flex' and `bison'.  All text in this section, starting at the
  502. final `%%' and extending to the end of the input file, is included
  503. verbatim into the generated output file.  Naturally, it is your
  504. responsibility to ensure that the code contained in this section is
  505. valid C.
  506. File: gperf.info,  Node: Output Format,  Prev: Input Format,  Up: Description
  507. Output Format for Generated C Code with `gperf'
  508. ===============================================
  509.    Several options control how the generated C code appears on the
  510. standard output.  Two C function are generated.  They are called `hash'
  511. and `in_word_set', although you may modify the name for `in_word_set'
  512. with a command-line option.  Both functions require two arguments, a
  513. string, `char *' STR, and a length parameter, `int' LEN.  Their default
  514. function prototypes are as follows:
  515.      static int hash (char *str, int len);
  516.      int in_word_set (char *str, int len);
  517.    By default, the generated `hash' function returns an integer value
  518. created by adding LEN to several user-specified STR key positions
  519. indexed into an "associated values" table stored in a local static
  520. array.  The associated values table is constructed internally by
  521. `gperf' and later output as a static local C array called HASH_TABLE;
  522. its meaning and properties are described below.  *Note
  523. Implementation::. The relevant key positions are specified via the `-k'
  524. option when running `gperf', as detailed in the *Options* section
  525. below. *Note Options::.
  526.    Two options, `-g' (assume you are compiling with GNU C and its
  527. `inline' feature) and `-a' (assume ANSI C-style function prototypes),
  528. alter the content of both the generated `hash' and `in_word_set'
  529. routines.  However, function `in_word_set' may be modified more
  530. extensively, in response to your option settings.  The options that
  531. affect the `in_word_set' structure are:
  532.     `-p'
  533.           Have function `in_word_set' return a pointer rather than a
  534.           boolean.
  535.     `-t'
  536.           Make use of the user-defined `struct'.
  537.     `-S TOTAL SWITCH STATEMENTS'
  538.           Generate 1 or more C `switch' statement rather than use a
  539.           large, (and potentially sparse) static array.  Although the
  540.           exact time and space savings of this approach vary according
  541.           to your C compiler's degree of optimization, this method
  542.           often results in smaller and faster code.
  543.    If the `-t', `-S', and `-p' options are omitted the default action
  544. is to generate a `char *' array containing the keys, together with
  545. additional null strings used for padding the array.  By experimenting
  546. with the various input and output options, and timing the resulting C
  547. code, you can determine the best option choices for different keyword
  548. set characteristics.
  549. File: gperf.info,  Node: Options,  Next: Bugs,  Prev: Description,  Up: Top
  550. Options to the `gperf' Utility
  551. ******************************
  552.    There are *many* options to `gperf'.  They were added to make the
  553. program more convenient for use with real applications.  "On-line" help
  554. is readily available via the `-h' option.  Other options include:
  555.     `-a'
  556.           Generate ANSI Standard C code using function prototypes.  The
  557.           default is to use "classic" K&R C function declaration syntax.
  558.     `-c'
  559.           Generates C code that uses the `strncmp' function to perform
  560.           string comparisons.  The default action is to use `strcmp'.
  561.     `-C'
  562.           Makes the contents of all generated lookup tables constant,
  563.           *i.e.*, "readonly."  Many compilers can generate more
  564.           efficient code for this by putting the tables in readonly
  565.           memory.
  566.     `-d'
  567.           Enables the debugging option.  This produces verbose
  568.           diagnostics to "standard error" when `gperf' is executing.
  569.           It is useful both for maintaining the program and for
  570.           determining whether a given set of options is actually
  571.           speeding up the search for a solution.  Some useful
  572.           information is dumped at the end of the program when the `-d'
  573.           option is enabled.
  574.     `-D'
  575.           Handle keywords whose key position sets hash to duplicate
  576.           values.  Duplicate hash values occur for two reasons:
  577.              * Since `gperf' does not backtrack it is possible for it
  578.                to process all your input keywords without finding a
  579.                unique mapping for each word.  However, frequently only
  580.                a very small number of duplicates occur, and the
  581.                majority of keys still require one probe into the table.
  582.              * Sometimes a set of keys may have the same names, but
  583.                possess different attributes.  With the -D option
  584.                `gperf' treats all these keys as part of an equivalence
  585.                class and generates a perfect hash function with multiple
  586.                comparisons for duplicate keys.  It is up to you to
  587.                completely disambiguate the keywords by modifying the
  588.                generated C code.  However, `gperf' helps you out by
  589.                organizing the output.
  590.           Option `-D' is extremely useful for certain large or highly
  591.           redundant keyword sets, *i.e.*, assembler instruction opcodes.
  592.           Using this option usually means that the generated hash
  593.           function is no longer perfect.  On the other hand, it permits
  594.           `gperf' to work on keyword sets that it otherwise could not
  595.           handle.
  596.     `-e KEYWORD DELIMITER LIST'
  597.           Allows the user to provide a string containing delimiters
  598.           used to separate keywords from their attributes.  The default
  599.           is ",\n".  This option is essential if you want to use
  600.           keywords that have embedded commas or newlines.  One useful
  601.           trick is to use -e'TAB', where TAB is the literal tab
  602.           character.
  603.     `-E'
  604.           Define constant values using an enum local to the lookup
  605.           function rather than with #defines.  This also means that
  606.           different lookup functions can reside in the same file.
  607.           Thanks to James Clark (jjc at ai.mit.edu).
  608.     `-f ITERATION AMOUNT'
  609.           Generate the perfect hash function "fast."  This decreases
  610.           `gperf''s running time at the cost of minimizing generated
  611.           table-size.  The iteration amount represents the number of
  612.           times to iterate when resolving a collision.  `0' means
  613.           `iterate by the number of keywords.  This option is probably
  614.           most useful when used in conjunction with options `-D' and/or
  615.           `-S' for *large* keyword sets.
  616.     `-g'
  617.           Assume a GNU compiler, *e.g.*, `g++' or `gcc'.  This makes
  618.           all generated routines use the "inline" keyword to remove the
  619.           cost of function calls.  Note that `-g' does *not* imply
  620.           `-a', since other non-ANSI C compilers may have provisions
  621.           for a function `inline' feature.
  622.     `-G'
  623.           Generate the static table of keywords as a static global
  624.           variable, rather than hiding it inside of the lookup function
  625.           (which is the default behavior).
  626.     `-h'
  627.           Prints a short summary on the meaning of each program option.
  628.           Aborts further program execution.
  629.     `-H HASH FUNCTION NAME'
  630.           Allows you to specify the name for the generated hash
  631.           function.  Default name is `hash.'  This option permits the
  632.           use of two hash tables in the same file.
  633.     `-i INITIAL VALUE'
  634.           Provides an initial VALUE for the associate values array.
  635.           Default is 0.  Increasing the initial value helps inflate the
  636.           final table size, possibly leading to more time efficient
  637.           keyword lookups.  Note that this option is not particularly
  638.           useful when `-S' is used.  Also, `-i' is overriden when the
  639.           `-r' option is used.
  640.     `-j JUMP VALUE'
  641.           Affects the "jump value," *i.e.*, how far to advance the
  642.           associated character value upon collisions.  JUMP VALUE is
  643.           rounded up to an odd number, the default is 5.  If the JUMP
  644.           VALUE is 0 `gper f' jumps by random amounts.
  645.     `-k KEYS'
  646.           Allows selection of the character key positions used in the
  647.           keywords' hash function. The allowable choices range between
  648.           1-126, inclusive.  The positions are separated by commas,
  649.           *e.g.*, `-k 9,4,13,14'; ranges may be used, *e.g.*, `-k 2-7';
  650.           and positions may occur in any order.  Furthermore, the
  651.           meta-character '*' causes the generated hash function to
  652.           consider *all* character positions in each key, whereas '$'
  653.           instructs the hash function to use the "final character" of a
  654.           key (this is the only way to use a character position greater
  655.           than 126, incidentally).
  656.           For instance, the option `-k 1,2,4,6-10,'$'' generates a hash
  657.           function that considers positions 1,2,4,6,7,8,9,10, plus the
  658.           last character in each key (which may differ for each key,
  659.           obviously).  Keys with length less than the indicated key
  660.           positions work properly, since selected key positions
  661.           exceeding the key length are simply not referenced in the
  662.           hash function.
  663.     `-K KEY NAME'
  664.           By default, the program assumes the structure component
  665.           identifier for the keyword is "name."  This option allows an
  666.           arbitrary choice of identifier for this component, although
  667.           it still must occur as the first field in your supplied
  668.           `struct'.
  669.     `-l'
  670.           Compare key lengths before trying a string comparison.  This
  671.           might cut down on the number of string comparisons made
  672.           during the lookup, since keys with different lengths are
  673.           never compared via `strcmp'.  However, using `-l' might
  674.           greatly increase the size of the generated C code if the
  675.           lookup table range is large (which implies that the switch
  676.           option `-S' is not enabled), since the length table contains
  677.           as many elements as there are entries in the lookup table.
  678.     `-L GENERATED LANGUAGE NAME'
  679.           Instructs `gperf' to generate code in the language specified
  680.           by the option's argument.  Languages handled are currently
  681.           C++ and C.  The default is C.
  682.     `-n'
  683.           Instructs the generator not to include the length of a
  684.           keyword when computing its hash value.  This may save a few
  685.           assembly instructions in the generated lookup table.
  686.     `-N LOOKUP FUNCTION NAME'
  687.           Allows you to specify the name for the generated lookup
  688.           function.  Default name is `in_word_set.'  This option
  689.           permits completely automatic generation of perfect hash
  690.           functions, especially when multiple generated hash functions
  691.           are used in the same application.
  692.     `-o'
  693.           Reorders the keywords by sorting the keywords so that
  694.           frequently occuring key position set components appear first.
  695.           A second reordering pass follows so that keys with "already
  696.           determined values" are placed towards the front of the
  697.           keylist.  This may decrease the time required to generate a
  698.           perfect hash function for many keyword sets, and also produce
  699.           more minimal perfect hash functions.  The reason for this is
  700.           that the reordering helps prune the search time by handling
  701.           inevitable collisions early in the search process.  On the
  702.           other hand, if the number of keywords is *very* large using
  703.           `-o' may *increase* `gperf''s execution time, since
  704.           collisions will begin earlier and continue throughout the
  705.           remainder of keyword processing.  See Cichelli's paper from
  706.           the January 1980 Communications of the ACM for details.
  707.     `-p'
  708.           Changes the return value of the generated function
  709.           `in_word_set' from boolean (*i.e.*, 0 or 1), to either type
  710.           "pointer to user-defined struct," (if the `-t' option is
  711.           enabled), or simply to `char *', if `-t' is not enabled.
  712.           This option is most useful when the `-t' option (allowing
  713.           user-defined structs) is used.  For example, it is possible
  714.           to automatically generate the GNU C reserved word lookup
  715.           routine with the options `-p' and `-t'.
  716.     `-r'
  717.           Utilizes randomness to initialize the associated values
  718.           table.  This frequently generates solutions faster than using
  719.           deterministic initialization (which starts all associated
  720.           values at 0).  Furthermore, using the randomization option
  721.           generally increases the size of the table.  If `gperf' has
  722.           difficultly with a certain keyword set try using `-r' or `-D'.
  723.     `-s SIZE-MULTIPLE'
  724.           Affects the size of the generated hash table.  The numeric
  725.           argument for this option indicates "how many times larger or
  726.           smaller" the maximum associated value range should be, in
  727.           relationship to the number of keys.  If the SIZE-MULTIPLE is
  728.           negative the maximum associated value is calculated by
  729.           *dividing* it into the total number of keys.  For example, a
  730.           value of 3 means "allow the maximum associated value to be
  731.           about 3 times larger than the number of input keys."
  732.           Conversely, a value of -3 means "allow the maximum associated
  733.           value to be about 3 times smaller than the number of input
  734.           keys."  Negative values are useful for limiting the overall
  735.           size of the generated hash table, though this usually
  736.           increases the number of duplicate hash values.
  737.           If `generate switch' option `-S' is *not* enabled, the maximum
  738.           associated value influences the static array table size, and
  739.           a larger table should decrease the time required for an
  740.           unsuccessful search, at the expense of extra table space.
  741.           The default value is 1, thus the default maximum associated
  742.           value about the same size as the number of keys (for
  743.           efficiency, the maximum associated value is always rounded up
  744.           to a power of 2).  The actual table size may vary somewhat,
  745.           since this technique is essentially a heuristic.  In
  746.           particular, setting this value too high slows down `gperf''s
  747.           runtime, since it must search through a much larger range of
  748.           values.  Judicious use of the `-f' option helps alleviate this
  749.           overhead, however.
  750.     `-S TOTAL SWITCH STATEMENTS'
  751.           Causes the generated C code to use a `switch' statement
  752.           scheme, rather than an array lookup table.  This can lead to
  753.           a reduction in both time and space requirements for some
  754.           keyfiles.  The argument to this option determines how many
  755.           `switch' statements are generated. A value of 1 generates 1
  756.           `switch' containing all the elements, a value of 2 generates
  757.           2 tables with 1/2 the elements in each `switch', etc.  This
  758.           is useful since many C compilers cannot correctly generate
  759.           code for large `switch' statements. This option was inspired
  760.           in part by Keith Bostic's original C program.
  761.     `-t'
  762.           Allows you to include a `struct' type declaration for
  763.           generated code.  Any text before a pair of consecutive %% is
  764.           consider part of the type declaration.  Key words and
  765.           additional fields may follow this, one group of fields per
  766.           line.  A set of examples for generating perfect hash tables
  767.           and functions for Ada, C, and G++, Pascal, and Modula 2 and 3
  768.           reserved words are distributed with this release.
  769.     `-T'
  770.           Prevents the transfer of the type declaration to the output
  771.           file.  Use this option if the type is already defined
  772.           elsewhere.
  773.     `-v'
  774.           Prints out the current version number.
  775.     `-Z CLASS NAME'
  776.           Allow user to specify name of generated C++ class.  Default
  777.           name is `Perfect_Hash'.
  778. File: gperf.info,  Node: Bugs,  Next: Projects,  Prev: Options,  Up: Top
  779. Known Bugs and Limitations with `gperf'
  780. ***************************************
  781.    The following are some limitations with the current release of
  782. `gperf':
  783.    * The `gperf' utility is tuned to execute quickly, and works quickly
  784.      for small to medium size data sets (around 1000 keywords).  It is
  785.      extremely useful for maintaining perfect hash functions for
  786.      compiler keyword sets.  Several recent enhancements now enable
  787.      `gperf' to work efficiently on much larger keyword sets (over
  788.      15,000 keywords).  When processing large keyword sets it helps
  789.      greatly to have over 8 megs of RAM.
  790.      However, since `gperf' does not backtrack no guaranteed solution
  791.      occurs on every run.  On the other hand, it is usually easy to
  792.      obtain a solution by varying the option parameters.  In
  793.      particular, try the `-r' option, and also try changing the default
  794.      arguments to the `-s' and `-j' options.  To *guarantee* a
  795.      solution, use the `-D' and `-S' options, although the final
  796.      results are not likely to be a *perfect* hash function anymore!
  797.      Finally, use the `-f' option if you want `gperf' to generate the
  798.      perfect hash function *fast*, with less emphasis on making it
  799.      minimal.
  800.    * The size of the generate static keyword array can get *extremely*
  801.      large if the input keyword file is large or if the keywords are
  802.      quite similar.  This tends to slow down the compilation of the
  803.      generated C code, and *greatly* inflates the object code size.  If
  804.      this situation occurs, consider using the `-S' option to reduce
  805.      data size, potentially increasing keyword recognition time a
  806.      negligible amount.  Since many C compilers cannot correctly
  807.      generated code for large switch statements it is important to
  808.      qualify the -S option with an appropriate numerical argument that
  809.      controls the number of switch statements generated.
  810.    * The maximum number of key positions selected for a given key has an
  811.      arbitrary limit of 126.  This restriction should be removed, and if
  812.      anyone considers this a problem write me and let me know so I can
  813.      remove the constraint.
  814.    * The C++ source code only compiles correctly with GNU G++, version
  815.      1.36 (and hopefully later versions).  Porting to AT&T cfront would
  816.      be tedious, but possible (and desirable).  There is also a K&R C
  817.      version available now.  This should compile without change on most
  818.      BSD systems, but may require a bit of work to run on SYSV, since
  819.      `gperf' uses ALLOCA in several places.  Send mail to schmidt at
  820.      ics.uci.edu for information.
  821. File: gperf.info,  Node: Projects,  Next: Implementation,  Prev: Bugs,  Up: Top
  822. Things Still Left to Do
  823. ***********************
  824.    It should be "relatively" easy to replace the current perfect hash
  825. function algorithm with a more exhaustive approach; the perfect hash
  826. module is essential independent from other program modules.  Additional
  827. worthwhile improvements include:
  828.    * Make the algorithm more robust.  At present, the program halts
  829.      with an error diagnostic if it can't find a direct solution and
  830.      the `-D' option is not enabled.  A more comprehensive, albeit
  831.      computationally expensive, approach would employ backtracking or
  832.      enable alternative options and retry.  It's not clear how helpful
  833.      this would be, in general, since most search sets are rather small
  834.      in practice.
  835.    * Another useful extension involves modifying the program to generate
  836.      "minimal" perfect hash functions (under certain circumstances, the
  837.      current version can be rather extravagant in the generated table
  838.      size).  Again, this is mostly of theoretical interest, since a
  839.      sparse table often produces faster lookups, and use of the `-S'
  840.      `switch' option can minimize the data size, at the expense of
  841.      slightly longer lookups (note that the gcc compiler generally
  842.      produces good code for `switch' statements, reducing the need for
  843.      more complex schemes).
  844.    * In addition to improving the algorithm, it would also be useful to
  845.      generate a C++ class or Ada package as the code output, in
  846.      addition to the current C routines.
  847. File: gperf.info,  Node: Implementation,  Next: Bibliography,  Prev: Projects,  Up: Top
  848. Implementation Details of GNU `gperf'
  849. *************************************
  850.    A paper describing the high-level description of the data structures
  851. and algorithms used to implement `gperf' will soon be available.  This
  852. paper is useful not only from a maintenance and enhancement perspective,
  853. but also because they demonstrate several clever and useful programming
  854. techniques, *e.g.*, `Iteration Number' boolean arrays, double hashing,
  855. a "safe" and efficient method for reading arbitrarily long input from a
  856. file, and a provably optimal algorithm for simultaneously determining
  857. both the minimum and maximum elements in a list.
  858. File: gperf.info,  Node: Bibliography,  Prev: Implementation,  Up: Top
  859. Bibliography
  860. ************
  861.    [1] Chang, C.C.: A Scheme for Constructing Ordered Minimal Perfect
  862. Hashing Functions Information Sciences 39(1986), 187-195.
  863.    [2] Cichelli, Richard J. Author's Response to "On Cichelli's Minimal
  864. Perfec t Hash Functions Method" Communications of the ACM, 23,
  865. 12(December 1980), 729.
  866.    [3] Cichelli, Richard J. Minimal Perfect Hash Functions Made Simple
  867. Communications of the ACM, 23, 1(January 1980), 17-19.
  868.    [4] Cook, C. R. and Oldehoeft, R.R. A Letter Oriented Minimal
  869. Perfect Hashing Function SIGPLAN Notices, 17, 9(September 1982), 18-27.
  870.    [5] Cormack, G. V. and Horspool, R. N. S. and Kaiserwerth, M.
  871. Practical Perfect Hashing Computer Journal, 28, 1(January 1985), 54-58.
  872.    [6] Jaeschke, G. Reciprocal Hashing: A Method for Generating Minimal
  873. Perfect Hashing Functions Communications of the ACM, 24, 12(December
  874. 1981), 829-833.
  875.    [7] Jaeschke, G. and Osterburg, G. On Cichelli's Minimal Perfect
  876. Hash Functions Method Communications of the ACM, 23, 12(December 1980),
  877. 728-729.
  878.    [8] Sager, Thomas J. A Polynomial Time Generator for Minimal Perfect
  879. Hash Functions Communications of the ACM, 28, 5(December 1985), 523-532
  880.    [9] Schmidt, Douglas C. GPERF: A Perfect Hash Function Generator
  881. Second USENIX C++ Conference Proceedings, April 1990.
  882.    [10] Sebesta, R.W. and Taylor, M.A. Minimal Perfect Hash Functions
  883. for Reserved Word Lists  SIGPLAN Notices, 20, 12(September 1985), 47-53.
  884.    [11] Sprugnoli, R. Perfect Hashing Functions: A Single Probe
  885. Retrieving Method for Static Sets Communications of the ACM, 20
  886. 11(November 1977), 841-850.
  887.    [12] Stallman, Richard M. Using and Porting GNU CC Free Software
  888. Foundation, 1988.
  889.    [13] Stroustrup, Bjarne The C++ Programming Language.
  890. Addison-Wesley, 1986.
  891.    [14] Tiemann, Michael D. User's Guide to GNU C++ Free Software
  892. Foundation, 1989.
  893. Tag Table:
  894. Node: Top
  895. Node: Copying
  896. Node: Contributors
  897. 15742
  898. Node: Motivation
  899. 16842
  900. Node: Search Structures
  901. 18109
  902. Node: Description
  903. 21662
  904. Node: Input Format
  905. 23482
  906. Node: Declarations
  907. 24277
  908. Node: Keywords
  909. 26584
  910. Node: Functions
  911. 28175
  912. Node: Output Format
  913. 28669
  914. Node: Options
  915. 31139
  916. Node: Bugs
  917. 44509
  918. Node: Projects
  919. 47196
  920. Node: Implementation
  921. 48773
  922. Node: Bibliography
  923. 49492
  924. End Tag Table
  925.